// pages/topic/topic.js
import { TOPIC_DETAIL } from "../../request/api";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0,
    title: '',
    joinCount: 0,
    desc: '',
    tabIndex: 0,
    tabnav: {
      tabitem: [{
          "id": 0,
          "text": "最热"
        },
        {
          "id": 1,
          "text": "最新"
        },
      ]
    },
    hotData: {
      type: 2,
      name: '',
      loadStatus: 2,
    },
    newData: {
      type: 1,
      name: '',
      loadStatus: 2,
    },
    winHeight: 0,
    // titleBarHeight: 0,
    // statusBarHeight: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var globalData = getApp().globalData;
    var winHeight = globalData.windowHeight;
    // var titleBarHeight = globalData.windowHeight;
    // var statusBarHeight = globalData.windowHeight;
    var name = options.name;
    var id = options.id || 0;
    if(name){
      wx.setNavigationBarTitle({
        title: name,
      });
      that.setData({
        id: id,
        title: name,
        winHeight: winHeight,
        hotData: {
          type: 2,
          id: id,
          name: name,
          height: winHeight - 50,
        },
        newData: {
          type: 1,
          id: id,
          name: name,
          height: winHeight - 50,
        },
      });
    } else {
      that.setData({
        winHeight: winHeight,
      });
    }
    this.getTopicDetail(id, name);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    // 设置菜单中的转发按钮触发转发事件时的转发内容
    var shareObj = {
      title: that.getShareTitle(), // 默认是小程序的名称(可以写slogan等)
      path: '/pages/topic/topic?id=' + that.data.id + '&name=' + that.data.title, // 默认是当前页面，必须是以‘/’开头的完整路径
      // imageUrl: '', //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径，支持PNG及JPG，不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
      success: function (res) {
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {}
      },
    };
    return shareObj;
  },
  onShareTimeline: function(){
    var that = this;
    // 设置菜单中的转发按钮触发转发事件时的转发内容
    var shareObj = {
      title: that.getShareTitle(), // 默认是小程序的名称(可以写slogan等)
      query: 'id=' + that.data.id + '&name=' + that.data.title, //当前页面路径携带的参数
      success: function (res) {
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {}
      },
    };
    return shareObj;
  },

  getShareTitle: function(){
    var userInfo = getApp().globalData.userInfo;
    var userName;
    if(userInfo){
      userName = userInfo.name || '有人';
    } else {
      userName = '有人';
    }
    var title = userName + '分享了话题：'+ this.data.title;
    return title;
  },

    /** 
   * 滑动切换tab 
   */
  bindChange: function (e) {
    this.setData({
      tabIndex: e.detail.current
    });
  },
  /** 
   * 点击tab切换 
   */
  setTab: function (e) {
    const edata = e.currentTarget.dataset;
    this.setData({
      tabIndex: edata.tabindex,
    });
  },

  getTopicDetail: function(id, name){
    TOPIC_DETAIL({'id': id, 'name': name}).then(res => {
      if(res.data.code == 1){
        var result = res.data.result;
        this.setData({
          id: result.id,
          title: result.title,
          joinCount: result.joinCount,
          desc: result.desc,
        });
      }
    });
  }
})